package com.construction.api.controller;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class DataResponse<T> implements Serializable {
    public static final int ERROR_CODE = 300;
    private static final long serialVersionUID = 1L;
    private T data;
    private int code = 100;
    public static final int OK_CODE = 100;
    private String message;

    public DataResponse(T data) {
        this.data = data;
    }

    public DataResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public DataResponse(String message) {
        this.message = message;
        this.code = ERROR_CODE;
    }

    public static DataResponse<Object> ok(Object data) {
        DataResponse<Object> response = new DataResponse<>(OK_CODE, "");
        response.setData(data);
        return response;
    }

    public static DataResponse<Object> success(String mes) {
        return new DataResponse<>(OK_CODE, mes);
    }

    public static DataResponse<Object> error(String mes) {
        return new DataResponse<>(ERROR_CODE, mes);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
